package com.wenan.ui.controls.command
{
	import com.wenan.AppGlobal;
	import com.wenan.data.PageInfo;
	import com.wenan.data.Parameter;
	import com.wenan.ui.reports.ReportDataEvent;
	import com.wenan.ui.reports.ReportDataFactory;
	
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.Image;
	
	/**
	 * 报表图标
	 * */
	public class StatisticsButtonBar extends HBox
	{
		private var _statisticsReports:Array;
		private var _parentPageInfo:PageInfo;
		
		public function StatisticsButtonBar()
		{
			super();
			setStyle("verticalAlign", "middle");
		}

		public function get parentPageInfo():PageInfo
		{
			return _parentPageInfo;
		}
		
		public function set parentPageInfo(value:PageInfo):void
		{
			_parentPageInfo = value;
		}
		
		public function get statisticsReports():Array
		{
			return _statisticsReports;
		}

		public function set statisticsReports(value:Array):void
		{
			removeAllElements();
			
			_statisticsReports = value;
			
			buildElements();
		}
		
		private function buildElements():void
		{
			if (_statisticsReports)
			{
				var image:Image;
				for each(var statisticsReport:Object in _statisticsReports)
				{
					image = new Image();
					image.source = statisticsReport.icon;
					image.data = statisticsReport;
					image.toolTip = statisticsReport.reportType;
					image.useHandCursor = true;
					image.buttonMode = true;
					image.addEventListener(MouseEvent.CLICK, clickHandler);
					addElement(image);
				}
			}
		}
		
		private function clickHandler(event:MouseEvent):void
		{
			var resultData:Object = event.currentTarget.data as Object;
			
			if (AppGlobal.modulePageContainer)
			{
				if (resultData)
				{
					var _selectedName:String = resultData.reportName;
					if(_selectedName == null || _selectedName == "") return ;
					
					var isouter:String = resultData.plugurl as String;
					if (isouter && isouter != "")
					{
//						var url:String = obj.url;
						//加载外部swf文件
					} else 
					{
						var reportDataFactory:ReportDataFactory = new ReportDataFactory();
						reportDataFactory.getReportItemData(_selectedName);
						reportDataFactory.addEventListener(ReportDataEvent.REPORTDATA, function(event:ReportDataEvent):void {
							var pageInfo:PageInfo = event.pageInfo;
							pageInfo.reportIntoWay = "statisticsButtonBar";
							AppGlobal.modulePageContainer.navigate(pageInfo, true);	
						});
					}
				}	
			}	
		}
		
		private function getStatisticsReportPageInfo(statisticsReport:Object):PageInfo
		{
			var pageInfo:PageInfo = new PageInfo("Report", statisticsReport.positionId, 
				statisticsReport.reportType, statisticsReport.positionType);
			pageInfo.parameters.add(new Parameter("ReportType", statisticsReport.reportType));
			pageInfo.parameters.add(new Parameter("navName", statisticsReport.reportType));
			return pageInfo;
		}

		

	}
}